package test;

import static com.google.common.collect.Maps.newLinkedHashMap;
import static net.ftng.commons.lang.utils.PrintUtils.println;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.ftng.commons.lang.utils.StringUtils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class QQLoginBy3G {

    public static void main(final String[] args) throws Exception {

        String qq = "63389114";
        String pwd = ".wuwa.";
        String loginPageUrl = "http://pt.3g.qq.com/?sid=ASClxOOtgi-bjczs2qVa9SNv";

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpGet httpGet = new HttpGet(loginPageUrl);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        String responseBody = EntityUtils.toString(entity);
        String string = StringUtils.getStringsByRegex(responseBody, "<go href.+method").get(1);
        String loginAction = string.replace("<go href=\"", "").replace("\" method", "");
        Map<String, String> fields = getFieldAndValues(responseBody);
        fields.put("qq", qq);
        fields.put("defaultQQ", qq);
        fields.put("pwd", pwd);
        fields.put("loginType", "3");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for (Entry<String, String> nameValuePair : fields.entrySet()) {
            nvps.add(new BasicNameValuePair(nameValuePair.getKey(), nameValuePair.getValue()));
        }
        HttpPost httpPost = new HttpPost(loginAction);
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse httpResponse2 = httpClient.execute(httpPost);
        HttpEntity entity2 = httpResponse2.getEntity();
        String responseBody2 = EntityUtils.toString(entity2);
        String sid = StringUtils.getStringsByRegex(responseBody2, "sid=.+").get(0).substring(4, 28);
        println(sid);
    }

    private static Map<String, String> getFieldAndValues(final String source) {

        List<String> fields = StringUtils.getStringsByRegex(source, "<postfield name=\".+/>");
        Map<String, String> fieldsAndValues = newLinkedHashMap();
        for (String fieldText : fields) {

            String link = fieldText.replaceAll(".*ame=\"", "").replaceAll("\" /.*", "");
            String[] keyAndValues = link.split("\" value=\"");
            String key = keyAndValues[0];
            fieldsAndValues.put(key, keyAndValues.length == 1 ? "" : keyAndValues[1]);
            if (key.equals("login_url"))
                break;
        }
        return fieldsAndValues;
    }
}
